<%@ page language="java" pageEncoding="utf-8" contentType="text/html; charset=utf-8"%>
<%@ taglib prefix="ww" uri="webwork" %>
<ww:bean name="'com.css.apps.base.dict.service.DictMan'" id="dictID" />
<ww:bean name="'com.css.apps.base.common.provider.SysQueryProvider'" id="sysQuery" />

<form class="form-horizontal form-validate" name="form1" id="form1" method="post">

	<%-- 如果一个领导有多个秘书, 并且有多个系统的不同种类领导, 则通过此处直接关联秘书会不合适, 
		应新建领导秘书关联模块, 建立不同系统、不同种类领导(直属领导/分管领导...)、不同秘书之间的关联关系更好维护 --%>
	<!-- <a class="btn" onclick="addLeaderType();" style="margin: 0 20px">添加</a> -->
	<div style="height: 250px; width: 550px; overflow: auto;">
		<ul id="tree" class="ztree"></ul>
	</div>
	<div class="set-btn" data-spy="affix" data-offset-top="200">
		<a class="btn green" onclick="submitSelect();">保存</a>
		<a class="btn" href="javascript:;" onclick="$css.closeDialog()">取消</a>
	</div>
</form>
<script>
	var tree, lsType = '<ww:property value="lsType" />';

	using('tree', function() {
		var res = '<ww:property value="tree" />', $d = $css.focusDialog(), $tab = $navTab.focus();
		if ("branchDept" === lsType) {
			$('#wrapper', $d).hide();
		}
		if (!isnull(res)) {
			var arr = JSON.parse(res), setting = {
				check: {
					enable: true,
					chkboxType: { "Y": "s", "N": "s" }
				},
				data : {
					simpleData : {
						enable : true
					}
				}
			};
			tree = $.fn.zTree.init($('#tree', $d), setting, arr);
			if ('branchDept' === lsType) {
				var removeNodes = tree.getNodesByParam("name", "书记处", null);
				for ( var node in removeNodes) {
					tree.removeNode(removeNodes[node]);
				}
			}
		}
	});
	
	function submitSelect() {
		var $tab = $navTab.focus(), $d = $css.focusDialog(), json = $('#secretaryJson', $tab).val();
		var selected = tree.getCheckedNodes(true), names = "",
			isSecretary = 'secretary' === lsType, isBranchDept = 'branchDept' === lsType, arr = isnull(json) ? [] : JSON.parse(json);
		var len = selected.length;
		for ( var idx in selected) {
			var node = selected[idx];
			var item = {}
			names += node.name;
			if (idx < len - 1) {
				names += '、';
			}
			if(isSecretary && !(node.isParent)) {
				item.secretaryId = node.id;
				item.secretaryName = node.name;
				item.secretaryDeptId = node.pId;
				item.branchDeptId = "";
				item.branchDeptName = "";
				$('#secretaryNames', $tab).val(names);
			} else if (isBranchDept) {
				item.secretaryId = "";
				item.secretaryName = "";
				item.secretaryDeptId = "";
				item.branchDeptId = node.id;
				item.branchDeptName = node.name;
				$('#branchDeptNames', $tab).val(names);
			}
			arr.push(item);
		}
		$('#secretaryJson', $tab).val(JSON.stringify(arr));
		$css.closeDialog();
	}
	
	function addLeaderType() {
		if (!isnull(selectItems)) {
			var $d = $css.focusDialog();
			var ltList = selectItems.leaderTypes, syss = selectItems.syss;
			var html = '<tr>';
			html += '<td><span class="required" style="color: #e02222;">*</span>领导类型</td>';
			html += '<td><select id="leaderType" class="input-small">';
			for ( var idx in ltList) {
				html += '<option value="'+ltList[idx].id+'">'+ltList[idx].name+'</option>';
			}
			html += '</select></td>';
			html += '<td><span class="required" style="color: #e02222;">*</span>系统选择</td>';
			html += '<td><select id="sysId" class="input-small" style="width: 150px;">';
			for ( var idx in ltList) {
				html += '<option value="'+ltList[idx].id+'">'+ltList[idx].name+'</option>';
			}
			html += '</select></td>';
			html += '<td><span class="required" style="color: #e02222;">*</span>排序号</td>';
			html += '<td><input id="orderNum" class="required input-small digits" /></td>';
			html += "</tr>";
			$('#wrapper', $d).append(html);
		}
	}
	
</script>